package cn.zwx.activiti.example;

import cn.hutool.core.collection.CollectionUtil;
import cn.zwx.activiti.example.manger.ProcessImageManager;
import cn.zwx.activiti.example.model.User;
import cn.zwx.activiti.example.utils.SecurityUtil;
import org.activiti.api.task.model.builders.TaskPayloadBuilder;
import org.activiti.api.task.runtime.TaskRuntime;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskInfoQuery;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
/**
 * @description 包含网关 等于并行网关+排他网关 可以在线条上添加条件
 **/
@SpringBootTest
@RunWith(SpringRunner.class)
class ActivitiExampleApplicationForInclude {

    @Autowired
    private SecurityUtil securityUtil;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskRuntime taskRuntime;

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private ProcessImageManager processImageManager;

    /**
     * @description 部署工作流
     * @return void
     * @author zhangwenxue
     * @createTime 2021/5/6 21:28
     **/
    @Test
    void loadProcess() {
        securityUtil.logInAs("张三");
        // 部署
        Deployment deployment = repositoryService.createDeployment().addClasspathResource("processes/examination.bpmn")
                .addClasspathResource("processes/examination.png").name("体检流程").deploy();
        // 打印信息
        System.out.println(deployment.getName());
        System.out.println(deployment.getId());
    }


    /**
     * @description 启动流程实例
     * @return void
     * @author zhangwenxue
     * @createTime 2021/5/6 21:28
     **/
    @Test
    void startProcess() {
        securityUtil.logInAs("李四");
        Map<String, Object> user = CollectionUtil.newHashMap();
        user.put("user",new User("李四",3,1));
        // 请假表业务id
        Long businessKey = 1L;
        // 启动流程
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("examination", businessKey.toString(),user);
        // 填写请假单姓名
//        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("jiantingqi",variables);
        // 打印信息
        System.out.println("DeploymentId:"+processInstance.getDeploymentId());
        System.out.println("流程实例id:"+processInstance.getId());
        System.out.println("流程定义id:"+processInstance.getProcessDefinitionId());
    }

    /**
     * @description 组任务 处理
     * @return void
     * @author zhangwenxue
     * @createTime 2021/5/17 20:31
     **/
    @Test
    void groupTaskTest(){
        securityUtil.logInAs("张三");
        // 查询组任务
        TaskInfoQuery taskQuery = taskService.createTaskQuery();
        List<Task> groupList = taskQuery.taskAssignee("张三").orderByTaskCreateTime().asc().list();
//        List<Task> groupList = taskQuery.taskCandidateUser("张三").orderByTaskCreateTime().asc().list();
        for (Task task : groupList) {
            System.out.println("group_list start ===========================");
            System.out.println("流程实例id"+task.getProcessInstanceId());
            System.out.println("任务id:"+task.getId());
            System.out.println("部署id"+task.getProcessDefinitionId());
            System.out.println("任务负责人:"+task.getAssignee());
            System.out.println("任务名称"+task.getName());
            System.out.println("group_list end =============================");
//             抓起任务
//            org.activiti.api.task.model.Task claim_task = taskRuntime.claim(TaskPayloadBuilder.claim().withTaskId(task.getId()).build());
//            taskService.claim();
//            System.out.println("claim_task start ===========================");
//            System.out.println("流程实例id"+claim_task.getProcessInstanceId());
//            System.out.println("任务id:"+claim_task.getId());
//            System.out.println("部署id"+claim_task.getProcessDefinitionId());
//            System.out.println("任务负责人:"+claim_task.getAssignee());
//            System.out.println("任务名称"+claim_task.getName());
//            System.out.println("抓起事件"+claim_task.getClaimedDate());
//            System.out.println("claim_task end =============================");
//
//            Map<String, Object> hashMap = CollectionUtil.newHashMap();
//            hashMap.put("audit_state",Boolean.TRUE);
//            hashMap.put("dayNum",4);
//            hashMap.put("leader_user","XueXue");
            // 完成任务
//            taskRuntime.complete(TaskPayloadBuilder.complete().withTaskId(task.getId()).withVariables(hashMap).build());
            taskRuntime.complete(TaskPayloadBuilder.complete().withTaskId(task.getId()).build());
//            taskService.complete();

        }
    }

    /**
     * @description 实现流程图当前节点跟踪
     * @return void
     * @author zhangwenxue
     * @createTime 2021/5/19 22:25
     **/
    @Test
    void image() throws Exception {
//        String procInstId = "7b12ace2-b8a8-11eb-adb3-5c80b6df2b95";
        String procInstId = "0a05e9a9-b96f-11eb-87c9-5c80b6df2b95";
//        String procInstId = "f8f0e571-b96e-11eb-a2e3-5c80b6df2b95";
        InputStream in = processImageManager.getFlowImgByProcInstId(procInstId);
        // 写出去 
        BufferedInputStream buffer_in = new BufferedInputStream(in);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] bytes = new byte[1024];
        int len = 0;
        while ((len = buffer_in.read(bytes))!= -1){
            // 写出去
            out.write(bytes,0,len);
        }
        FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\105286\\Desktop\\activiti-example\\src\\main\\resources\\processes\\examination_1.html");
        fileOutputStream.write(out.toByteArray());

        in.close();
        out.close();
        fileOutputStream.close();

    }

}
